var wholeCheck = document.querySelector('.whole-check');
var shopCheck = document.querySelectorAll('.shop-check');
var sonCheck = document.querySelectorAll('.son-check');
var buyNum = document.querySelector('.buy-num');
var payPrice = document.querySelector('.pay-price');
var input = document.querySelectorAll('label');
var priceSum = document.querySelectorAll('.price-sum');
var add = document.querySelectorAll('.add');
var reduce = document.querySelectorAll('.reduce');
var dele = document.querySelectorAll('.cart-dele');


// 总计
function total() {
    var num = 0;
    var price = 0;
    sonCheck.forEach(function(value, index) {
        // 当商品选中时总计数变化
        if (value.nextElementSibling.className == 'active') {
            num += Number(value.parentNode.parentNode.children[3].children[0].children[1].value);
            price += Number(priceSum[index].innerText);
        }
    });
    // 赋值
    buyNum.innerText = num;
    payPrice.innerText = price;
}


// 小计
function amount() {
    priceSum.forEach(function(value, index) {
        value.innerText = value.parentNode.parentNode.children[2].children[0].innerText * value.parentNode.parentNode.children[3].children[0].children[1].value;
        // 输入框输入时递归,并调用total
        value.parentNode.parentNode.children[3].children[0].children[1].oninput = function() {
            amount();
            total();
        }
    })
}
amount();


// 全选
var wholeflag = true;
wholeCheck.addEventListener('click', function() {
    if (wholeflag) {
        input.forEach(function(value, index) {
            value.className = 'active';
        })
        wholeflag = false;
    } else {
        input.forEach(function(value, index) {
            value.className = '';
        })
        wholeflag = true;
    }
    total();
})


// 店铺
var shopflag = true;
shopCheck.forEach(function(value, index) {
    var temp = value.parentNode.parentNode.nextElementSibling.children;
    value.addEventListener('click', function() {
        if (shopflag) {
            value.nextElementSibling.className = 'active';
            for (var i = 0; i < temp.length; i++) {
                temp[i].children[0].children[0].nextElementSibling.className = 'active';
            }
            shopflag = false;
        } else {
            value.nextElementSibling.className = '';
            for (var i = 0; i < temp.length; i++) {
                temp[i].children[0].children[0].nextElementSibling.className = '';
            }
            shopflag = true;
        }
        total();
        // 调用联动函数
        shopWhole();
    })
})

// 店铺与全选联动
function shopWhole() {
    for (var i = 0; i < shopCheck.length; i++) {
        if (shopCheck[i].nextElementSibling.className == 'active') {
            if (i == shopCheck.length - 1) {
                wholeCheck.nextElementSibling.className = 'active';
            }
            continue;
        } else {
            wholeCheck.nextElementSibling.className = '';
            break;
        }
    }
}


// 单个商品
var sonflag = true;
sonCheck.forEach(function(value, index) {
    value.addEventListener('click', function() {
        if (sonflag) {
            value.nextElementSibling.className = 'active';
            sonflag = false;
        } else {
            value.nextElementSibling.className = '';
            sonflag = true;
        }
        total();
        // 调用联动函数
        sonShop();
        shopWhole();
    })
})

// 商品与店铺联动
function sonShop() {
    for (var i = 0; i < shopCheck.length; i++) {
        var sons = shopCheck[i].parentNode.parentNode.nextElementSibling.children;
        for (var j = 0; j < sons.length; j++) {
            if (sons[j].children[0].children[0].nextElementSibling.className == 'active') {
                if (j == sons.length - 1) {
                    shopCheck[i].nextElementSibling.className = 'active';
                }
                continue;
            } else {
                shopCheck[i].nextElementSibling.className = '';
                break;
            }
        }
    }
}


// 加减数量
add.forEach(function(value, index) {
    value.addEventListener('click', function() {
        console.log(value.previousElementSibling.value);
        value.previousElementSibling.value = Number(value.previousElementSibling.value) + 1;
        value.previousElementSibling.previousElementSibling.disabled = false;
        // 调用小计与总计
        amount();
        total();
    })
})
reduce.forEach(function(value, index) {
    value.addEventListener('click', function() {
        value.nextElementSibling.value = Number(value.nextElementSibling.value) - 1;
        if (value.nextElementSibling.value == 1) {
            value.disabled = true;
        }
        // 调用小计与总计
        amount();
        total();
    })
})


// 删除
dele.forEach(function(value, index) {
    value.addEventListener('click', function() {
        value.parentNode.parentNode.remove();
        // 再次获取sonCheck
        sonCheck = document.querySelectorAll('.son-check');
        // 调用小计与总计
        amount();
        total();
    })
})